package LeetCode.month01.day20;

import java.util.Arrays;

public class No2090 {
    public static void main(String[] args) {
        System.out.println(Arrays.toString(getAverages(new int[]{2,2,1},1)));
    }
    public static int[] getAverages(int[] nums, int k) {
        int len = nums.length;
        int[] res = new int[len];
        if(k <= 0) return nums;
        if(k >= (len+1)/2) {
            Arrays.fill(res, -1);
            return res;
        }
        int windowSize = 2*k + 1;
        if(windowSize > len) {
            Arrays.fill(res, -1);
            return res;
        }
        double sum = 0;
        for(int i = 0; i < windowSize; i++) {
            sum += nums[i];
        }
        Arrays.fill(res,-1);
        for(int i = k; i < len - k; i++) {
            res[i] = (int)(sum / windowSize);
            if(i + k + 1 < len) {
                sum += nums[i + k + 1] - nums[i - k];
            }
        }
        return res;
    }
}
